package com.stone.schedule.common.util;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * Created by stone on 2017/8/31.
 */
public final class BeanValidator {

	private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();


	/**
	 *
	 * @param object
	 * @param groups
	 * @return
	 */
	public static String validate(Object object, Class<?>[] groups) {
		StringBuffer sb = new StringBuffer();
		final Set<ConstraintViolation<Object>> set = geValidator().validate(object, groups);
		for (ConstraintViolation constraintViolation : set) {
			sb.append(constraintViolation.getMessage());
		}
		return sb.toString();
	}

	/**
	 *
	 * @return
	 */
	private static Validator geValidator() {
		return validatorFactory.getValidator();
	}
}
